package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import glue.*;

public class EventNewFile extends JMenuItem implements ActionListener 
{
	Scuttlebutt parent = null;
	public EventNewFile(String text, Scuttlebutt parent)
	{
		super(text);
		this.parent = parent;
		addActionListener(this);
	}

	public void actionPerformed(ActionEvent a0)
	{
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle("New File");
		FolderNode o = null;
		Object obj = (Object)parent.getProject().getTree().getLastSelectedPathComponent();
		if(obj == null){
			chooser.setCurrentDirectory(new File(parent.getProject().getProjectPath().getPath() + File.separatorChar + parent.getProject().getProjectTitle()));
			o = (FolderNode)parent.getProject().getTreeModel().getRoot();
		}else{
			if (obj instanceof FolderNode ) {
				o = (FolderNode)obj;
			}else{
				o = (FolderNode)((SortableNode)obj).getParent();
			}
			chooser.setCurrentDirectory(new File(o.getNodePath()));
		}

		chooser.setMultiSelectionEnabled(false);
		chooser.addChoosableFileFilter(new SuperFileFilter(new String("Text"), new String("txt")));
		chooser.addChoosableFileFilter(new SuperFileFilter(new String("Java server page"), new String("jsp")));
		chooser.addChoosableFileFilter(new SuperFileFilter(new String("Python"), new String("py")));
		chooser.addChoosableFileFilter(new SuperFileFilter(new String("HTML"), new String("htm,html")));
		chooser.setAcceptAllFileFilterUsed(false);

		int returnVal = chooser.showSaveDialog(parent);
		if(returnVal == JFileChooser.APPROVE_OPTION){
			String ext = null;
			SuperFileFilter filter = (SuperFileFilter)chooser.getFileFilter();
			if(filter.getPossibleExtensions().contains("htm")){
				ext = new String(".htm");
			}else if(filter.getPossibleExtensions().contains("jsp")){
				ext = new String(".jsp");
			}else if(filter.getPossibleExtensions().contains("py")){
				ext = new String(".py");
			}else if(filter.getPossibleExtensions().contains("txt")){
				ext = new String(".txt");
			}else{
				ext = new String(".htm");
			}
			
			File f = new File(chooser.getSelectedFile().getPath() + ext);
			createFile(f, o);
		}
	}
	
	private void createFile(File file, FolderNode o){
		if((o != null) && (o.getNodePath() == null)){
			System.err.println("dir is null");
			return;
		}
	
		if(parent.getProject() != null){
			try{
				file.createNewFile();
			}catch (IOException e){
				System.err.println("failed to create file:" + e.getMessage());
			}
			
			
			
			SortableNode node = new SortableNode(file.getName(), file.getPath());
			parent.getEditPane().Open(file.getPath());
			node.setUploaded(false);
			
			String remote_dir = null;
			if(o.getRemoteDirectory().compareToIgnoreCase(parent.getRemoteHomeDirectory()) == 0){
				remote_dir = new String("");
			}else if(o.getRemoteDirectory().length() == 0){
				remote_dir = new String(o.toString());
			}else{
				remote_dir = new String(o.getRemoteDirectory() + File.separatorChar + o.toString());
			}
			node.setRemoteDirectory(remote_dir);
 		   	o.addSortable(node);
 		   	parent.updateTree();
		}
		parent.saveProject();	
	}
}

